package online.heycm;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.builder.Entity;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.Collections;
import java.util.ResourceBundle;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        ResourceBundle rb = ResourceBundle.getBundle("code-generator");
        String outPutDir = rb.getString("outPutDir");
        String xmlFollowMapper = rb.getString("xmlFollowMapper");
        String xmlOutPutDir = rb.getString("xmlOutPutDir");
        String parentPackage = rb.getString("parentPackage");
        String moduleName = rb.getString("moduleName");
        String author = rb.getString("author");
        String dbDriver = rb.getString("dataSource.driverName");
        String dbUsername = rb.getString("dataSource.username");
        String dbPassword = rb.getString("dataSource.password");
        String dbUrl = rb.getString("dataSource.url");
        String fileOverwrite = rb.getString("file.overwrite");
        String tablePrefix = rb.getString("tablePrefix");
        String fieldPrefix = rb.getString("fieldPrefix");
        String isUseSwagger2 = rb.getString("isUseSwagger2");
        String isLombokModel = rb.getString("isLombokModel");
        String insert_update = rb.getString("insert_update");
        String insert = rb.getString("insert");
        String update = rb.getString("update");
        String delete = rb.getString("delete");
        String tables = rb.getString("tables");


        FastAutoGenerator.create(dbUrl, dbUsername, dbPassword)
                .globalConfig(builder -> {
                    builder.author(author)
                            .dateType(DateType.ONLY_DATE)
                            .outputDir(outPutDir);
                    if ("true".equals(isUseSwagger2)) {
                        builder.enableSwagger();
                    }
                })
                .packageConfig(builder -> {
                    builder.parent(parentPackage)
                            .moduleName(moduleName);
                    if ("true".equals(xmlFollowMapper)) {
                        String xmlPath = (outPutDir + "/" + parentPackage + "/" + moduleName + "/mapper/xml")
                                .replaceAll("\\.", "/");
                        builder.pathInfo(Collections.singletonMap(OutputFile.xml, xmlPath));
                    } else {
                        builder.pathInfo(Collections.singletonMap(OutputFile.xml, xmlOutPutDir));
                    }
                })
                .strategyConfig(builder -> {
                    if ("true".equals(fileOverwrite)) {
                        builder.controllerBuilder().fileOverride();
                        builder.serviceBuilder().fileOverride();
                        builder.mapperBuilder().fileOverride();
                        builder.entityBuilder().fileOverride();
                    }
                    String[] tabs = tables.split(",");
                    if (tabs.length > 0) {
                        builder.addInclude(tabs);
                    }
                    String[] tabPre = tablePrefix.split(",");
                    if (tabPre.length > 0) {
                        builder.addTablePrefix(tabPre);
                    }
                    String[] fiePre = fieldPrefix.split(",");
                    if (fiePre.length > 0) {
                        builder.addFieldPrefix(fiePre);
                    }
                    builder.controllerBuilder().enableRestStyle();
                    Entity.Builder entityBuilder = builder.entityBuilder();
                    entityBuilder.naming(NamingStrategy.underline_to_camel);
                    if ("true".equals(isLombokModel)) {
                        entityBuilder.enableLombok();
                    }
                    String[] iuFill = insert_update.split(",");
                    if (iuFill.length > 0) {
                        for (String iu : iuFill) {
                            entityBuilder.addTableFills(new Column(iu, FieldFill.INSERT_UPDATE));
                        }
                    }
                    String[] iFill = insert.split(",");
                    if (iFill.length > 0) {
                        for (String i : iFill) {
                            entityBuilder.addTableFills(new Column(i, FieldFill.INSERT));
                        }
                    }
                    String[] uFill = update.split(",");
                    if (uFill.length > 0) {
                        for (String u : uFill) {
                            entityBuilder.addTableFills(new Column(u, FieldFill.UPDATE));
                        }
                    }
                    if (!delete.isBlank()) {
                        entityBuilder.logicDeleteColumnName(delete);
                    }
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

}
